package cn.b.sky.mes.cm.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.b.sky.mes.cm.constants.CmConstant;
import cn.b.sky.mes.cm.enums.OperationType;
import cn.b.sky.mes.cm.model.Operation;
import cn.b.sky.mes.cm.model.ProcessAttachment;
import cn.b.sky.mes.cm.model.ProcessCapacity;
import cn.b.sky.mes.cm.model.ProcessComponent;
import cn.b.sky.mes.cm.model.ProcessInspectPicture;
import cn.b.sky.mes.cm.model.ProcessModel;
import cn.b.sky.mes.cm.model.ProcessOperation;
import cn.b.sky.mes.cm.model.condition.ProcessCondition;
import cn.b.sky.mes.cm.service.OperationService;
import cn.b.sky.mes.cm.service.ProcessService;
import cn.b.sky.mes.cm.service.WorkCenterTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.enums.ParameterEnums;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.ParameterService;
import cn.b.sky.user.service.model.ECode;
import cn.b.sky.user.service.model.ParameterEntity;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * <strong>Title : ProcessController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.cm.controller<br></strong>
 * <strong>Description : </strong>工艺工序控制器<br> 
 * <strong>Create on : 2015-9-30 上午10:35:09<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("process")
public class ProcessController extends SecurityController {

  private static final Logger LOGGER = LoggerFactory.getLogger(ProcessController.class);

  @Resource
  private ProcessService processService;

  @Resource
  private WorkCenterTypeService workCenterTypeService;

  @Resource
  private OperationService operationService;

  @Resource
  private ECodeService eCodeService;
  
  @Resource
  private ParameterService parameterService;

  @RequestMapping("processList")
  public String processList(Model model, String navTabId, @ModelAttribute("condition") ProcessCondition condition) {
    if (condition == null) {
      condition = new ProcessCondition();
    }
    condition.setSiteCode(this.getSiteCode());
    model.addAttribute("workCenterType", workCenterTypeService.selectAllWorkCenterType(this.getSiteCode()));
    model.addAttribute("page", processService.getProcessPageByCondition(condition));
    model.addAttribute(NAVTABID, navTabId);
    return "cm/process/process_list";
  }

  @RequestMapping("addProcess")
  public String addProcess(Model model, String navTabId, HttpSession session) {
    //model.addAttribute("workCenterType", workCenterTypeService.selectAllWorkCenterType(this.getSiteCode()));
    //删除临时数据考虑查询出来数据库中存在的工艺工序
    //新增前检查临时数据是否存在，不存在则新增一个
    processService.checkTempProcessByPk(this.getSiteCode(), CmConstant.PROCESSCODE_TEMP,
        CmConstant.PROCESSREV_TEMP);
    ProcessOperation pop = new ProcessOperation();
    pop.setCreateUser(SecurityContextUtil.getCurrentUserName());
    pop.setSiteCode(this.getSiteCode());
    //临时赋值
    pop.setProcessCode(CmConstant.PROCESSCODE_TEMP);
    pop.setProcessRev(CmConstant.PROCESSREV_TEMP);

    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTCOLLECTMODE.getCode(),
        this.getCurrentLanguage());
    model.addAttribute("sessionid", session.getId());

    model.addAttribute("defectcollectMode", ecode);
    try {
      processService.deleteTempProcessOperation(pop);
    } catch (Exception e) {
      LOGGER.error("{}", e);
    }
    model.addAttribute(NAVTABID, navTabId);
    return "cm/process/process_add";
  }

  @RequestMapping("copyProcess")
  public String copyProcess(Model model, String navTabId, String id, HttpSession session) {
    if (StringUtils.isBlank(id)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    if (id.indexOf(SystemConstant.MIDDLE_SPLIT) != -1) {
      String[] str = id.split(SystemConstant.MIDDLE_SPLIT);
      //id 是processCode+"@_@"+processrev
      if (str.length == 2) {
        //考虑查询出来数据库中存在的工艺工序 删除临时数据
        //新增前检查临时数据是否存在，不存在则新增一个
        processService.checkTempProcessByPk(this.getSiteCode(), CmConstant.PROCESSCODE_TEMP,
            CmConstant.PROCESSREV_TEMP);
        ProcessOperation pop = new ProcessOperation();
        pop.setCreateUser(SecurityContextUtil.getCurrentUserName());
        pop.setSiteCode(this.getSiteCode());
        //临时赋值
        pop.setProcessCode(CmConstant.PROCESSCODE_TEMP);
        pop.setProcessRev(CmConstant.PROCESSREV_TEMP);
        //删除临时数据
        try {
          processService.deleteTempProcessOperation(pop);
        } catch (Exception e) {
          LOGGER.error("{}", e);
        }
        ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(), str[0], str[1]);
        
        List<ProcessOperation> pops = processService.copyProcessOperationByProcessPk(pm.getSiteCode(),
            pm.getProcessCode(), pm.getProcessRev());
        model.addAttribute("processModel", pm);
        model.addAttribute("processOperations", pops);
        String processOprationIds = ",";
        //复制记录工艺工序id
        for (ProcessOperation p : pops) {
          processOprationIds += p.getId();
          processOprationIds += ",";
        }
        //采集类型
        List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTCOLLECTMODE.getCode(),
            this.getCurrentLanguage());
        model.addAttribute("defectcollectMode", ecode);
        model.addAttribute("processOprationIds", processOprationIds);
        //工序类型
        List<ECode> operationTypeECode = eCodeService.selectECodeList(ECodeEnums.OPERATIONTYPE.getCode(),
            SecurityContextUtil.getLanguageCode());
        model.addAttribute("operationTypeECode", operationTypeECode);
      }
    }
    model.addAttribute("sessionid", session.getId());
    model.addAttribute(NAVTABID, navTabId);
    return "cm/process/process_copy";
  }

  //发布
  @RequestMapping("releaseProcess")
  @ResponseBody
  public DWZResult releaseProcess(Model model, String navTabId, String id) {
    if (StringUtils.isBlank(id)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    if (id.indexOf(SystemConstant.MIDDLE_SPLIT) != -1) {
      String[] str = id.split(SystemConstant.MIDDLE_SPLIT);
      //id 是processCode+"@_@"+processrev
      if (str.length == 2) {
        ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(), str[0], str[1]);
        if (pm != null && pm.getReleaseFlag() == 1) {
          return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "processIsRelease.M"));
        }
        pm = new ProcessModel();
        pm.setProcessCode(str[0]);
        pm.setProcessRev(str[1]);
        pm.setSiteCode(this.getSiteCode());
        pm.setLockFlag(null);
        pm.setReleaseFlag(1);
        processService.modifyProcess(pm, null, null);
      }
    } else {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterError.M"));
    }
    return navTabAjaxDone(navTabId, null, null, null, null, null);
  }

  @RequestMapping("deleteProcess")
  @ResponseBody
  public DWZResult deleteProcess(Model model, String navTabId, String id) {
    if (StringUtils.isBlank(id)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    if (id.indexOf(SystemConstant.MIDDLE_SPLIT) != -1) {
      String[] str = id.split(SystemConstant.MIDDLE_SPLIT);
      //id 是processCode+"@_@"+processrev
      if (str.length == 2) {
        ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(), str[0], str[1]);
        if (pm != null && pm.getReleaseFlag() == 1) {
          return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "processIsnotDelete.M"));
        }
        processService.deleteProcess(pm);
      }
    } else {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterError.M"));
    }
    return navTabAjaxDone(navTabId, null, null, null, null, null);
  }

  @RequestMapping("viewProcess")
  public String viewProcess(Model model, String navTabId, String id) {
    if (StringUtils.isBlank(id)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    if (id.indexOf(SystemConstant.MIDDLE_SPLIT) != -1) {
      String[] str = id.split(SystemConstant.MIDDLE_SPLIT);
      //id 是processCode+"@_@"+processrev
      if (str.length == 2) {
        ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(), str[0], str[1]);
        List<ProcessOperation> pops = processService.getProcessOperationByProcessCode(pm.getSiteCode(),
            pm.getProcessCode(), pm.getProcessRev());
        model.addAttribute("processModel", pm);
        model.addAttribute("processOperations", pops);
        model.addAttribute("workCenterType", workCenterTypeService.selectAllWorkCenterType(this.getSiteCode()));
        List<ECode> operationTypeECode = eCodeService.selectECodeList(ECodeEnums.OPERATIONTYPE.getCode(),
            SecurityContextUtil.getLanguageCode());
        model.addAttribute("operationTypeECode", operationTypeECode);
      }
    } else {
      throw ImeException.newException("parameterError.M");
    }
    //采集类型
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTCOLLECTMODE.getCode(),
        this.getCurrentLanguage());
    model.addAttribute("defectcollectMode", ecode);
    return "cm/process/process_view";
  }

  //编辑前事件
  @RequestMapping("checkedEditButtonEnable")
  @ResponseBody
  public DWZResult checkedEditButtonEnable(Model model, String navTabId, String id) {
    if (StringUtils.isBlank(id)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    if (id.indexOf(SystemConstant.MIDDLE_SPLIT) != -1) {
      String[] str = id.split(SystemConstant.MIDDLE_SPLIT);
      //id 是processCode+"@_@"+processrev
      if (str.length == 2) {
        ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(), str[0], str[1]);
        if (pm != null && pm.getReleaseFlag() == 1) {
          return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "processIsnotEdit.M"));
        }
      }
    } else {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterError.M"));
    }
    return ajaxDone(null);
  }

  @RequestMapping("editProcess")
  public String editProcess(Model model, String navTabId, String id, HttpSession session) {
    if (StringUtils.isBlank(id)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    if (id.indexOf(SystemConstant.MIDDLE_SPLIT) != -1) {
      String[] str = id.split(SystemConstant.MIDDLE_SPLIT);
      //id 是processCode+"@_@"+processrev
      if (str.length == 2) {
        ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(), str[0], str[1]);
        if (pm != null && pm.getReleaseFlag() == 1) {
          throw ImeException.newException("processIsnotEdit.M");
        }
        List<ProcessOperation> pops = processService.getProcessOperationByProcessCode(pm.getSiteCode(),
            pm.getProcessCode(), pm.getProcessRev());
        List<ECode> operationTypeECode = eCodeService.selectECodeList(ECodeEnums.OPERATIONTYPE.getCode(),
            SecurityContextUtil.getLanguageCode());
        model.addAttribute("operationTypeECode", operationTypeECode);
        model.addAttribute("processModel", pm);
        model.addAttribute("processOperations", pops);
        model.addAttribute(NAVTABID, navTabId);
        model.addAttribute("workCenterType", workCenterTypeService.selectAllWorkCenterType(this.getSiteCode()));
      }
    } else {
      throw ImeException.newException("parameterError.M");
    }
    //采集类型
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTCOLLECTMODE.getCode(),
        this.getCurrentLanguage());
    model.addAttribute("defectcollectMode", ecode);
    model.addAttribute("sessionid", session.getId());
    return "cm/process/process_edit";
  }

  @RequestMapping("addProcessOperation")
  public String addProcessOperation(Model model, String processCode, String processRev, HttpSession session) {
    if (!StringUtils.isBlank(processCode) && !StringUtils.isBlank(processRev)) {
      model.addAttribute("processCode", processCode);
      model.addAttribute("processRev", processRev);
    }
    //采集类型
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTCOLLECTMODE.getCode(),
        this.getCurrentLanguage());
    /**
     * 确认点勾选参数,1--表示勾选，其他值不是勾选
     */
    ParameterEntity entity = parameterService.getParameterEntityByCode(ParameterEnums.CONFIRMISSELECTED.getCode(), getSiteCode());
    model.addAttribute("confirmIsselected", "1".equals(entity.getDefaultValue()) ? true : false);
    model.addAttribute("defectcollectMode", ecode);
    model.addAttribute("sessionid", session.getId());
    return "cm/process/processOperation_add";
  }

  @RequestMapping("addProcessComponent")
  public String addProcessComponent(Integer popId, Model model) {
    if (popId != null) {
      model.addAttribute("popId", popId);
    }
    return "cm/process/processComponent_add";
  }
  
  @RequestMapping("addProcessCapacity")
  public String addProcessCapacity(Integer popId, Model model) {
    if (popId != null) {
      model.addAttribute("popId", popId);
    }
    return "cm/process/processCapacity_add";
  }

  @RequestMapping("editProcessOperation")
  public String editProcessOperation(Integer popId, Model model, HttpSession session) {
    if (popId == null) {
      LOGGER.error("工艺工序为空!");
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    ProcessOperation pop = processService.getProcessOperationById(popId);
    Operation operation = operationService.getOperation(pop.getOperationCode(), getSiteCode());
    ECode ecode = eCodeService.selectEcodeByECodeTypeAndLanguageCodeAndECode(operation.getOperationTypeEcode(),
        ECodeEnums.OPERATIONTYPE.getCode(), SecurityContextUtil.getLanguageCode());
    //填充附件
    pop.setAttachment(processService.getProcessAttachmentByProcessOperation(pop.getId()));
    pop.setCapacity(processService.getProcessCapacityByProcessOperation(pop.getId()));
    if (OperationType.PRODUCTION.getValue().equals(operation.getOperationTypeEcode())) {
      pop.setComponent(processService.getProcessComponentByProcessOperation(pop.getId()));
    } else if (OperationType.QUALITY.getValue().equals(operation.getOperationTypeEcode())
        && pop.getGraphcollectFlag() == 1) {
      pop.setPicture(processService.getProcessInspectPictureByProcessOperation(pop.getId()));
    }
    pop.setOperationTypeText(ecode.geteCodeText());
    //采集类型
    List<ECode> defectcollectMode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTCOLLECTMODE.getCode(),
        this.getCurrentLanguage());
    model.addAttribute("defectcollectMode", defectcollectMode);
    model.addAttribute("processOperation", pop);
    model.addAttribute("sessionid", session.getId());
    return "cm/process/processOperation_edit";
  }

  @RequestMapping("saveProcessOperation")
  @ResponseBody
  public DWZResult saveProcessOperation(ProcessOperation pop, Model model) {
    if (pop == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    pop.setSiteCode(this.getSiteCode());
    if (StringUtils.isBlank(pop.getProcessCode()) && pop.getId() == null) {
      pop.setProcessCode(CmConstant.PROCESSCODE_TEMP); //临时赋值
      pop.setProcessRev(CmConstant.PROCESSREV_TEMP);
    }
    setCreateInfo(pop);
    Operation operation = operationService.getOperation(pop.getOperationCode(), getSiteCode());
    pop.setOperationTypeEcode(operation.getOperationTypeEcode());
    ECode ecode = eCodeService.selectEcodeByECodeTypeAndLanguageCodeAndECode(operation.getOperationTypeEcode(),
        ECodeEnums.OPERATIONTYPE.getCode(), SecurityContextUtil.getLanguageCode());
    pop.setOperationTypeText(ecode.geteCodeText());
    this.processService.saveOrUpdateProcessOperation(pop);
    if (pop.getId() == null) {
      //保存失败
      return navTabAjaxError();
    }
    return ajaxDone(pop);
  }

  @RequestMapping("delProcessOpration")
  @ResponseBody
  public DWZResult delProcessOpration(Integer id) {
    if (id == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    this.processService.deleteProcessOperationById(id);
    return ajaxDone(null);
  }

  @RequestMapping("delProcessAttachment")
  @ResponseBody
  public DWZResult delProcessAttachment(Long id) {
    if (id == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    this.processService.deleteProcessAttachmentById(id);

    return ajaxDone(null);
  }

  @RequestMapping("delProcessInspectPic")
  @ResponseBody
  public DWZResult delProcessInspectPic(Long id) {
    if (id == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    this.processService.deleteProcessInspectPicById(id);

    return ajaxDone(null);
  }

  @RequestMapping("delProcessComponent")
  @ResponseBody
  public DWZResult delProcessComponent(Integer popId, String componentCode) {
    if (popId == null || StringUtils.isBlank(componentCode)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    this.processService.deleteProcessComponentByProcessOperation(popId, componentCode);

    return ajaxDone(null);
  }

  @RequestMapping("fechProcessAttachmentAndComment")
  @ResponseBody
  public DWZResult fechProcessAttachmentAndComment(Integer id) {
    if (id == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    ProcessOperation pop = this.processService.getProcessOperationById(id);
    if (pop != null && OperationType.QUALITY.getValue().equals(pop.getOperationTypeEcode())) {
      List<ProcessInspectPicture> pictures = this.processService.getProcessInspectPictureByProcessOperation(id);
      pop.setPicture(pictures);
    } else {
      List<ProcessComponent> component = this.processService.getProcessComponentByProcessOperation(id);
      pop.setComponent(component);
    }
    List<ProcessAttachment> attas = this.processService.getProcessAttachmentByProcessOperation(id);    
    pop.setAttachment(attas);
    List<ProcessCapacity> capacits = this.processService.getProcessCapacityByProcessOperation(id);
    pop.setCapacity(capacits);
    return ajaxDone(pop);
  }

  @RequestMapping("saveProcess")
  @ResponseBody
  public DWZResult saveProcess(String navTabId, ProcessModel processModel, String processOprationIds) {
    if (processModel == null || StringUtils.isBlank(processModel.getProcessCode())
        || StringUtils.isBlank(processModel.getProcessRev())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(), processModel.getProcessCode(),
        processModel.getProcessRev());
    if (pm != null && StringUtils.isNotBlank(pm.getProcessCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          "cmprocess_code.C,cmprocessrev.C,AlreadyExists.C"));
    }
    ProcessModel p = processService.getProcessBySCodeAndEffectiveDate(this.getSiteCode(),
        processModel.getProcessCode(), processModel.getEffectiveDate());
    if (p != null && StringUtils.isNotBlank(p.getProcessCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          "cmprocess_code.C,effectiveDate.C,AlreadyExists.C"));
    }
    setCreateInfo(processModel);
    processService.saveProcess(processModel, processOprationIds);
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("updateProcess")
  @ResponseBody
  public DWZResult updateProcess(String navTabId, ProcessModel processModel, String processOprationAddIds,
      String processOprationDelIds) {
    if (processModel == null || StringUtils.isBlank(processModel.getProcessCode())
        || StringUtils.isBlank(processModel.getProcessRev())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(processModel);
    if (StringUtils.isBlank(processModel.getProcessText())) {
      //置空
      processModel.setProcessText("");
    }
    processService.modifyProcess(processModel, processOprationAddIds, processOprationDelIds);
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping(value = "saveProcessAttachment")
  public void saveProcessAttachment(@RequestParam("processfile") MultipartFile processfile, HttpServletResponse response)
      throws IOException {
    response.setContentType("application/json;charset=UTF-8");
    ProcessAttachment processAttachment = new ProcessAttachment();
    processAttachment.setSiteCode(this.getSiteCode());
    processService.saveProcessAttachment(processAttachment, processfile);

    response.getWriter().print(JSONObject.toJSONString(ajaxDone(processAttachment)));
  }

  /**
   * 
   * <strong>Title : saveQualityProcessOperation<br></strong>
   * <strong>Description : </strong>保存质量工序设置<br> 
   * <strong>Create on : 2015-12-11 上午11:03:49<br></strong>
   * <p>
   * @param operation
   * @return
   * DWZResult
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "saveQualityProcessOperation")
  @ResponseBody
  public DWZResult saveQualityProcessOperation(ProcessOperation operation) {
    if (operation == null || operation.getId() == null) {
      LOGGER.error("工艺工序为空!");
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    if (StringUtils.isBlank(operation.getCollectModeEcode())) {
      LOGGER.error("质量工序的默认采集点为空!");
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    processService.saveOrUpdateProcessOperation(operation);
    return ajaxDone(null);
  }

  @RequestMapping(value = "saveProcessinspectpicture")
  public void saveProcessinspectpicture(@RequestParam("processPicture") MultipartFile processPicture,
      HttpServletResponse response) throws IOException {
    response.setContentType("application/json;charset=UTF-8");
    ProcessInspectPicture picture = new ProcessInspectPicture();
    picture.setSiteCode(this.getSiteCode());
    processService.saveProcessInspectPicture(picture, processPicture);
    response.getWriter().print(JSONObject.toJSONString(ajaxDone(picture)));
  }

  @RequestMapping("saveProcessComponent")
  @ResponseBody
  public DWZResult saveProcessComponent(ProcessComponent component, Integer popId, Model model) {
    if (popId == null) {
      LOGGER.error("工艺工序为空!");
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    component.setProcessOperationID(popId);
    ProcessOperation pop = processService.getProcessOperationById(popId);
    if (!"ProcessCode_TEMP".equals(pop.getProcessCode())
        && processService.checkProcessIsUsedByMaterial(pop.getProcessCode(), this.getSiteCode(),
            component.getComponentCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "isExistsProcessCodeInMaterial.M"));
    }
    processService.saveProcessComponent(component);

    return ajaxDone(component);
  }

  @RequestMapping("isExistsProcessCodeAndProcessrev")
  @ResponseBody
  public Boolean isExistsProcessCodeAndProcessrev(String processCode, String processRev) {
    if (StringUtils.isNotBlank(processCode) && StringUtils.isNotBlank(processRev)) {
      ProcessModel pm = processService.getProcessBySCodeAndPCode(this.getSiteCode(),
          StringUtils.upperCase(processCode), processRev);
      if (pm != null && StringUtils.isNotBlank(pm.getProcessCode())) {
        return false;
      }
    }
    return true;
  }
}
